package cn.sciento.wechat.service.impl;

import com.alibaba.fastjson.JSON;
import javax.annotation.Resource;
import cn.sciento.wechat.dto.AddMaterialResultDTO;
import cn.sciento.wechat.dto.AddMediaDTO;
import cn.sciento.wechat.dto.AddMediaResultDTO;
import cn.sciento.wechat.dto.DefaultResultDTO;
import cn.sciento.wechat.dto.DescriptionDTO;
import cn.sciento.wechat.dto.GetMaterialCountResultDTO;
import cn.sciento.wechat.dto.GetMaterialListDTO;
import cn.sciento.wechat.dto.GetMaterialListResultDTO;
import cn.sciento.wechat.dto.GetMaterialResultDTO;
import cn.sciento.wechat.dto.GetMediaResulDTO;
import cn.sciento.wechat.dto.MediaDTO;
import cn.sciento.wechat.dto.UpdateMaterialDTO;
import cn.sciento.wechat.dto.UploadImageMediaResultDTO;
import cn.sciento.wechat.dto.UploadMediaResultDTO;
import cn.sciento.wechat.service.WechatMaterialManageService;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

public class WechatMaterialManageServiceImp implements WechatMaterialManageService {
  @Resource(name = "wdRestTemplate")
  protected RestTemplate restTemplate;
  
  public UploadMediaResultDTO uploadMedia(String access_token, String type, MediaDTO media) {
    LinkedMultiValueMap linkedMultiValueMap = new LinkedMultiValueMap();
    linkedMultiValueMap.add("filename", media.getFilename());
    linkedMultiValueMap.add("Content-Type", media.getContentType());
    linkedMultiValueMap.add("contentLength", media.getFileLength());
    return (UploadMediaResultDTO)this.restTemplate.postForObject("https://api.weixin.qq.com/cgi-bin/media/upload?access_token=" + access_token + "&type=" + type, new HttpEntity(linkedMultiValueMap, (MultiValueMap)buildFormDateHttpHeaders()), UploadMediaResultDTO.class);
  }
  
  public ResponseEntity<GetMediaResulDTO> getMedia(String access_token, String media_id) {
    ResponseEntity<GetMediaResulDTO> exchange = this.restTemplate.exchange("https://api.weixin.qq.com/cgi-bin/media/get?access_token=" + access_token + "&media_id=" + media_id, HttpMethod.GET, null, GetMediaResulDTO.class);
    return exchange;
  }
  
  public ResponseEntity<DefaultResultDTO> getHDVoice(String access_token, String media_id) {
    ResponseEntity<DefaultResultDTO> exchange = this.restTemplate.exchange("https://api.weixin.qq.com/cgi-bin/media/get/jssdk?" + access_token + "& media_id=" + media_id, HttpMethod.GET, null, DefaultResultDTO.class);
    return exchange;
  }
  
  public AddMediaResultDTO addMedia(String access_token, AddMediaDTO addMediaDTO) {
    return (AddMediaResultDTO)this.restTemplate.postForObject("https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=" + access_token, new HttpEntity(JSON.toJSONString(addMediaDTO), (MultiValueMap)buildJsonHttpHeaders()), AddMediaResultDTO.class);
  }
  
  public UploadImageMediaResultDTO uploadImageMediaResultDTO(String access_token, MediaDTO mediaDTO) {
    return (UploadImageMediaResultDTO)this.restTemplate.postForObject("https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=" + access_token, new HttpEntity(JSON.toJSONString(mediaDTO), (MultiValueMap)buildJsonHttpHeaders()), UploadImageMediaResultDTO.class);
  }
  
  public AddMaterialResultDTO addMaterial(String access_token, String type, MediaDTO media, DescriptionDTO description) {
    LinkedMultiValueMap linkedMultiValueMap = new LinkedMultiValueMap();
    linkedMultiValueMap.add("filename", media.getFilename());
    linkedMultiValueMap.add("Content-Type", media.getContentType());
    if (description != null) {
      linkedMultiValueMap.add("title", description.getTitle());
      linkedMultiValueMap.add("introduction", description.getIntroduction());
    } 
    return (AddMaterialResultDTO)this.restTemplate.postForObject("https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=" + access_token, new HttpEntity(linkedMultiValueMap, (MultiValueMap)buildFormDateHttpHeaders()), AddMaterialResultDTO.class);
  }
  
  public GetMaterialResultDTO getMaterial(String access_token, String media_id) {
    return (GetMaterialResultDTO)this.restTemplate.postForObject("https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=" + access_token, new HttpEntity(JSON.toJSONString(media_id), (MultiValueMap)buildJsonHttpHeaders()), GetMaterialResultDTO.class);
  }
  
  public DefaultResultDTO deleteMaterial(String access_token, String media_id) {
    return (DefaultResultDTO)this.restTemplate.postForObject("https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=" + access_token, new HttpEntity(JSON.toJSONString(media_id), (MultiValueMap)buildJsonHttpHeaders()), DefaultResultDTO.class);
  }
  
  public DefaultResultDTO updateMaterial(String access_token, UpdateMaterialDTO updateMaterialDTO) {
    return (DefaultResultDTO)this.restTemplate.postForObject("https://api.weixin.qq.com/cgi-bin/material/update_news?access_token=" + access_token, new HttpEntity(JSON.toJSONString(updateMaterialDTO), (MultiValueMap)buildJsonHttpHeaders()), DefaultResultDTO.class);
  }
  
  public GetMaterialCountResultDTO getMaterialCount(String access_token) {
    return (GetMaterialCountResultDTO)this.restTemplate.getForObject("https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token=" + access_token, GetMaterialCountResultDTO.class);
  }
  
  public GetMaterialListResultDTO getMaterialList(String access_token, GetMaterialListDTO updateMaterialDTO) {
    return (GetMaterialListResultDTO)this.restTemplate.postForObject("https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" + access_token, new HttpEntity(JSON.toJSONString(updateMaterialDTO), (MultiValueMap)buildJsonHttpHeaders()), GetMaterialListResultDTO.class);
  }
  
  protected HttpHeaders buildFormDateHttpHeaders() {
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.set("Content-Type", "multipart/form-data");
    return httpHeaders;
  }
  
  protected HttpHeaders buildJsonHttpHeaders() {
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.set("Content-Type", "application/json");
    return httpHeaders;
  }
}
